package com.hc.bankonline.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.hc.bankonline.util.BaseDao;

public class BankOnlineDao {
	BaseDao baseDao = new BaseDao();
	public Map<String, Object> findLoginInfo(String loginAccount,String loginPassword){
		Map<String, Object> map = new HashMap<String, Object>();
		String sql = "SELECT id,cardno,balance FROM account WHERE cardno = ? and `password` = ?";
		
		Connection connection = baseDao.getConnection();
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, loginAccount);
			ps.setString(2, loginPassword);
			
			rs = ps.executeQuery();
			while(rs.next()){
				map.put("id", rs.getInt("id"));
				map.put("loginAccount", rs.getString("cardno"));	
				map.put("balance", rs.getInt("balance"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			baseDao.closeAll(connection, ps, rs);
		}
		return map;
	}
	public int findBalanceInfo(int id) {
		
		String sql = "SELECT balance FROM transaction_record WHERE id =? ;";
		
		Connection connection = baseDao.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				return rs.getInt("balance");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			baseDao.closeAll(connection, ps, rs);
		}
		return 0;
	}
	public int transferAddInfo(String cardno,int money) {
		String sql = "UPDATE account SET balance = (balance+?) WHERE cardno = ?;";
		
		Connection connection = baseDao.getConnection();
		PreparedStatement ps = null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, money);
			ps.setString(2, cardno);
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			baseDao.closeAll(connection, ps, null);
		}
		return 0;
	}
	public int transferDelInfo(String cardno,int money) {
		String sql = "UPDATE account SET balance = (balance-?) WHERE cardno = ?;";
		
		Connection connection = baseDao.getConnection();
		PreparedStatement ps = null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, money);
			ps.setString(2, cardno);
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			baseDao.closeAll(connection, ps, null);
		}
		return 0;
	}
	public int updatePwdInfo(String loginAccount, String loginPassword,
			String newPassword) {
		
		String sql = "UPDATE account SET password = ? WHERE cardno = ? AND password = ?";
		
		Connection connection = baseDao.getConnection();
		PreparedStatement ps = null;
		
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, newPassword);
			ps.setString(2, loginAccount);
			ps.setString(3, loginPassword);
			
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
	
}
